package com.molichuxing.services.business.convertor;

import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.business.dto.response.AccountLoginBizDto;
import com.molichuxing.services.infrastructure.dto.response.request.create.LoginLogCreateDto;
import com.molichuxing.services.infrastructure.dto.response.response.AccountDto;
import com.molichuxing.services.infrastructure.dto.response.response.AccountModifyDto;
import com.molichuxing.services.property.LoginLogTypeEnum;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.Map;

public class Convert {

    public static AccountLoginBizDto toAccountLoginBizDto(AccountDto beanDto) {
        AccountLoginBizDto beanBizDto = null;
        if (beanDto != null) {
            beanBizDto = new AccountLoginBizDto();
            BeanUtils.copyProperties(beanDto, beanBizDto);
            beanBizDto.setAccountId(beanDto.getId());
        }

        return beanBizDto;
    }

    public static AccountModifyDto toAccountModifyDto(AccountDto dto) {
        AccountModifyDto beanBizDto = null;
        if (dto != null) {
            beanBizDto = new AccountModifyDto();
            BeanUtils.copyProperties(dto, beanBizDto);
        }

        return beanBizDto;
    }

    public static LoginLogCreateDto toLoginLogCreateDto(AccountLoginBizDto accountLoginBizDto, Map<String, Object> params,
                                                        Boolean result, String resultExplain) {
        LoginLogCreateDto createDto = new LoginLogCreateDto();
        if (accountLoginBizDto != null) {
            createDto.setDealerId(accountLoginBizDto.getDealerId() == null ? 0 : accountLoginBizDto.getDealerId());
            createDto.setAccountId(accountLoginBizDto.getAccountId());
            createDto.setGroupId(accountLoginBizDto.getGroupId());
            createDto.setGroupName(accountLoginBizDto.getGroupName());
        }
        createDto.setAccountName(ObjectCastUtil.castString(params.get("account")));
        createDto.setBrowser(ObjectCastUtil.castString(params.get("browser")));
        createDto.setIpAddress(ObjectCastUtil.castString(params.get("requestIp")));
        createDto.setLoginAddress(ObjectCastUtil.castString(params.get("requestIpAddress")));
        createDto.setLoginTime(LocalDateTime.now());
        createDto.setType(LoginLogTypeEnum.LOGIN);
        createDto.setResult(result);
        createDto.setResultExplain(resultExplain);

        return createDto;
    }
}